<?php


namespace App\frontend\services\member;

use App\common\services\BaseServices;
use App\frontend\models\MemberModel;
use App\frontend\services\orders\OrderServices;
use Exception;
use Illuminate\Support\Str;

class MemberServices extends BaseServices
{

    //个人中心--订单流程
    public static $OrderProcessMenu = [
        'waitPay' => ['type_name' => '待付款', 'num' => 0],
        'waitSend' => ['type_name' => '待发货', 'num' => 0],
        'waitReceive' => ['type_name' => '待收货', 'num' => 0],
        'waitComment' => ['type_name' => '待评价', 'num' => 0],
        'afterSales' => ['type_name' => '售后', 'num' => 0]
    ];

    /**
     * 获取流程订单统计
     * @param $member_id
     * @return array
     */
    public static function getOrderProcessTotal($member_id)
    {
        $OrderProcess = self::$OrderProcessMenu;
        if (empty($member_id)) {
            return $OrderProcess;
        }
        //待付款
        $OrderProcess['waitPay']['num'] = OrderServices::getWaitPayTotal($member_id);
        //待发货
        $OrderProcess['waitSend']['num'] = OrderServices::getWaitSendTotal($member_id);
        //待收货
        $OrderProcess['waitReceive']['num'] = OrderServices::getWaitReceiveTotal($member_id);
        //待评价
        $OrderProcess['waitComment']['num'] = OrderServices::getWaitCommentTotal($member_id);
        //售后
        $OrderProcess['afterSales']['num'] = OrderServices::getafterSalesTotal($member_id);
        return $OrderProcess;
    }




    /**
     * 通过用户id获取用户数据
     * @param $userId
     * @param $filed
     * @return array|void
     */
    public static function getUserInfoById($userId, $filed)
    {
        $condition = ['uid' => $userId];
        $filed = !empty($filed) ? $filed : ['*'];
        $userInfo = MemberModel::getInstance()->getMemberInfo($condition, $filed);
        return $userInfo;
    }


    /**
     * 修改密码
     * @return void
     */
    public static function editPassword($old_pass, $new_pass, $confirm_pass, $userId)
    {
        if (empty($old_pass)) {
            return self::returnError(102, '请输入旧密码!');
        }
        if (empty($new_pass)) {
            return self::returnError(102, '请设置新密码!');
        }
        if (empty($confirm_pass)) {
            return self::returnError(102, '请再次确认密码!');
        }
        if ($new_pass != $confirm_pass) {
            return self::returnError(102, '确认密码与新密码不一致!');
        }
        $memberModel = MemberModel::getInstance();
        if (!$memberModel->passwordValidate($new_pass)) {
            return self::returnError(102, '密码格式不正确，请输入6-16位，数字，大小写字母组合!');
        }
        //查询旧密码
        $condition = ['uid' => $userId];
        $filed = ['password', 'salt'];
        $userInfo = $memberModel->getMemberInfo($condition, $filed);
        if ($userInfo['salt']) {
            return self::returnError(103, '您还未设置密码!');
        }
        //获取加密密码
        $old_pass_encrypt = $memberModel->passWordEncrypt($old_pass, $userInfo['salt']);
        if ($userInfo['password'] != $old_pass_encrypt) {
            return self::returnError(104, '旧密码输入错误!');
        }
        //保存
        $data['password'] = $memberModel->PassWordEncrypt($new_pass, $userInfo['salt']);
        $res = $memberModel->modifySave($condition, $data);
        if (!$res) {
            return self::returnError(105, '修改失败!');
        }
        return self::returnSuccess('修改成功!');
    }

    /**
     * 设置支付密码
     * @param $password
     * @param $userId
     * @return array
     */
    public static function setPayPassword($password, $userId)
    {
        if (empty($password)) {
            return self::returnError(202, '请输入支付密码!');
        }
        $memberModel = MemberModel::getInstance();
        if (!$memberModel->payPasswordValidate($password)) {
            return self::returnError(102, '密码格式不正确，请输入6位数字!');
        }

        //查询是否设置过密码
        $condition = ['uid' => $userId];
        $filed = ['pay_password', 'salt'];
        $userInfo = $memberModel->getMemberInfo($condition, $filed);
        if ($userInfo['pay_password']) {
            return self::returnError(400, '操作失误!');
        }

        //保存
        $data['pay_password'] = $memberModel->passWordEncrypt($password, $userInfo['salt']);
        $res = $memberModel->modifySave($condition, $data);
        if (!$res) {
            return self::returnError(203, '设置失败!');
        }
        return self::returnSuccess('设置成功!');
    }

    /**
     * 修改支付密码
     * @param $old_pass
     * @param $new_pass
     * @param $confirm_pass
     * @param $userId
     * @return array
     */
    public static function editPayPassword($old_pass, $new_pass, $confirm_pass, $userId)
    {
        if (empty($old_pass)) {
            return self::returnError(202, '请输入旧密码!');
        }
        if (empty($new_pass)) {
            return self::returnError(202, '请设置新密码!');
        }
        if (empty($confirm_pass)) {
            return self::returnError(202, '请再次确认密码!');
        }
        if ($new_pass != $confirm_pass) {
            return self::returnError(202, '确认密码与新密码不一致!');
        }
        $memberModel = MemberModel::getInstance();
        if (!$memberModel->payPasswordValidate($new_pass)) {
            return self::returnError(102, '密码格式不正确，请输入6位数字!');
        }
        //查询旧密码
        $condition = ['uid' => $userId];
        $filed = ['pay_password', 'salt'];
        $userInfo = $memberModel->getMemberInfo($condition, $filed);
        if ($userInfo['salt']) {
            return self::returnError(203, '您还未设置密码!');
        }
        //获取加密密码
        $old_pass_encrypt = $memberModel->passWordEncrypt($old_pass, $userInfo['salt']);
        if ($userInfo['pay_password'] != $old_pass_encrypt) {
            return self::returnError(204, '旧密码输入错误!');
        }
        //保存
        $data['pay_password'] = $memberModel->PassWordEncrypt($new_pass, $userInfo['salt']);
        $res = $memberModel->modifySave($condition, $data);
        if (!$res) {
            return self::returnError(205, '修改失败!');
        }
        return self::returnSuccess('修改成功!');
    }



}
